//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.trace;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import com.baidu.trace.api.fence.CircleFence;
import com.baidu.trace.api.fence.FenceShape;
import com.baidu.trace.c.d;
import com.baidu.trace.c.e;
import com.baidu.trace.model.CoordType;
import com.baidu.trace.model.LatLng;

final class w implements Runnable {
    w(u var1, int var2, int var3, SQLiteDatabase var4) {
        this.a = var2;
        this.b = var3;
        this.c = var4;
        super();
    }

    public final void run() {
        StringBuffer var1;
        (var1 = new StringBuffer("CREATE TABLE if not exists ")).append("trace_location (").append("loc_time INTEGER, ").append("entity_name VARCHAR(128), ").append("location_data BLOB);");
        StringBuffer var2;
        (var2 = new StringBuffer("CREATE TABLE if not exists ")).append("trace_fence (").append("fence_id INTEGER PRIMARY KEY AUTOINCREMENT, ").append("fence_name VARCHAR(128), ").append("monitored_person VARCHAR(128), ").append("coord_type VARCHAR(16), ").append("denoise INTEGER, ").append("fence_shape VARCHAR(16), ").append("create_time VARCHAR(32), ").append("modify_time VARCHAR(32), ").append("monitored_status VARCHAR(16), ").append("fence_extern_info TEXT);");
        StringBuffer var3;
        (var3 = new StringBuffer("CREATE TABLE if not exists ")).append("trace_fence_alarm (").append("fence_id INTEGER, ").append("fence_name VARCHAR(128), ").append("monitored_person VARCHAR(128), ").append("monitored_action VARCHAR(16), ").append("create_time INTEGER, ").append("cur_point TEXT, ").append("pre_point TEXT);");
        Cursor var4 = null;

        try {
            for(int var5 = this.a; var5 < this.b; ++var5) {
                switch(this.a) {
                    case 2:
                        this.c.execSQL(var3.toString());
                        StringBuffer var6;
                        (var6 = new StringBuffer("ALTER TABLE ")).append("trace_location RENAME TO ").append("trace_location_TEMP;");
                        this.c.execSQL(var6.toString());
                        this.c.execSQL(var1.toString());
                        (var6 = new StringBuffer("select * from ")).append("trace_location_TEMP;");
                        var4 = this.c.rawQuery(var6.toString(), (String[])null);
                        long var10;
                        String var21;
                        if(var4 != null) {
                            while(var4.moveToNext()) {
                                if(!TextUtils.isEmpty(var21 = var4.getString(var4.getColumnIndex("location_data"))) && var21.length() >= 8) {
                                    byte[] var7 = d.a(var21);
                                    var10 = Long.parseLong(var21.substring(var21.length() - 8, var21.length()), 16);
                                    (var6 = new StringBuffer("insert into ")).append("trace_location(").append("loc_time, ").append("entity_name, ").append("location_data) values(?,?,?);");
                                    Object[] var22 = new Object[]{Long.valueOf(var10), Trace.e, var7};
                                    this.c.execSQL(var6.toString(), var22);
                                }
                            }

                            var4.close();
                            var4 = null;
                        }

                        (var6 = new StringBuffer("drop table ")).append("trace_location_TEMP;");
                        this.c.execSQL(var6.toString());
                        (var6 = new StringBuffer("ALTER TABLE ")).append("trace_fence RENAME TO ").append("trace_fence_TEMP;");
                        this.c.execSQL(var6.toString());
                        this.c.execSQL(var2.toString());
                        StringBuffer var23;
                        (var23 = new StringBuffer("select * from ")).append("trace_fence_TEMP;");
                        var4 = this.c.rawQuery(var23.toString(), (String[])null);
                        if(var4 != null) {
                            label211:
                            while(true) {
                                int var8;
                                int var24;
                                do {
                                    do {
                                        if(!var4.moveToNext()) {
                                            var4.close();
                                            var4 = null;
                                            break label211;
                                        }

                                        var10 = var4.getLong(0);
                                        var24 = (var21 = var4.getString(1)).indexOf("[");
                                        var8 = var21.indexOf("]");
                                    } while(var24 <= 0);
                                } while(var8 <= 0);

                                String[] var25 = var21.substring(var24 + 1, var8).split(";");
                                ContentValues var26;
                                (var26 = new ContentValues()).put("monitored_person", Trace.e);
                                var26.put("fence_shape", FenceShape.circle.name());
                                var26.put("coord_type", CoordType.bd09ll.name());
                                var26.put("create_time", e.c());
                                var26.put("modify_time", e.c());
                                CircleFence var27 = CircleFence.buildLocalFence(var10, (String)null, (String)null, (LatLng)null, 0.0D, 0, (CoordType)null);
                                int var9 = (var25 = var25).length;

                                String[] var13;
                                for(int var12 = 0; var12 < var9; ++var12) {
                                    if((var13 = var25[var12].split("=")).length == 2) {
                                        String var14 = var13[0].trim();
                                        String var30 = var13[1].trim();
                                        if("name".equals(var14)) {
                                            var26.put("fence_name", var30);
                                        } else if("center".equals(var14)) {
                                            if((var13 = var30.split(",")).length == 2) {
                                                var27.setCenter(new LatLng(Double.parseDouble(var13[0]), Double.parseDouble(var13[1])));
                                            }
                                        } else if("radius".equals(var14)) {
                                            var27.setRadius(Double.parseDouble(var30));
                                        } else if("precision".equals(var14)) {
                                            var26.put("denoise", var30);
                                        }
                                    }
                                }

                                var6 = new StringBuffer();
                                a.a(var27, (StringBuffer)null, var6);
                                var26.put("fence_extern_info", var6.toString());

                                try {
                                    if(var10 > 0L) {
                                        ContentValues var28;
                                        (var28 = new ContentValues()).put("seq", Long.valueOf(var10 - 1L));
                                        StringBuffer var29;
                                        (var29 = new StringBuffer("name")).append(" like ? ");
                                        var13 = new String[]{"trace_fence"};
                                        this.c.update("sqlite_sequence", var28, var29.toString(), var13);
                                    }
                                } catch (Exception var18) {
                                    ;
                                }

                                this.c.insert("trace_fence", (String)null, var26);
                            }
                        }

                        (var6 = new StringBuffer("drop table ")).append("trace_fence_TEMP;");
                        this.c.execSQL(var6.toString());
                }
            }
        } catch (Exception var19) {
            ;
        } finally {
            if(var4 != null) {
                var4.close();
            }

        }

    }
}
